package com.heatup.apt.common.dao.redis;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.heatup.apt.common.dao.user.UserInfoMapper;
import com.heatup.apt.model.user.UserInfo;

@Component("userPositionRedisDao")
public class UserPositionRedisDao extends BaseRedisDao<Object>{
	
	private Logger logger = LoggerFactory.getLogger(UserPositionRedisDao.class);
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	/**
	 * 将用户的信息存入redis中
	 * @param userInfo
	 * @param map
	 */
	public void addUserPosition(String openId,Map<String,Object> map){
		this.setToken_vaild_time(24*3600);
		logger.info("============= add user position start:======="+openId);
		UserInfo userInfo = userInfoMapper.selectByOpenid(openId);
		if(userInfo != null && userInfo.getOpenid() != null){
			String key = userInfo.getOpenid();
			this.save(key, map);
		}
	}
	
	/**
	 * 获取用户地理位置信息
	 * @param userInfo
	 * @return
	 */
	public Map<String,Object> queryUserPosition(UserInfo userInfo){
		logger.info("================queryUserPosition: userInfo"+userInfo);
		Map<String,Object> mapRedis = null;
		if(userInfo != null && userInfo.getOpenid() != null){
			Object objRedis = this.get(userInfo.getOpenid(), Object.class);
			if(objRedis != null){
				mapRedis = (Map<String,Object>)objRedis;
			}else{
				logger.info("========== queryUserPosition: not user position======================");
			}
		}
		return mapRedis;
	}

}
